package com.jdk.learn.jvm;

import java.io.InputStream;

/**
 * 继承ClassLoad 复写loadClass方法
 */
public class _008ClassLoader extends ClassLoader {
    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        //name是类似com.ricky.User
        String  ClassName=name+".class";
        InputStream is=getClass().getResourceAsStream(ClassName);
        if (is==null) {
        return super.loadClass(name);
        }
        try {
            byte[] buff=new byte[is.available()];
            is.read(buff);
            return defineClass(name,buff,0,buff.length);
        } catch (Exception e) {

        }

        return super.loadClass(name);
    }

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        _008ClassLoader my=new _008ClassLoader();
        Object o = my.loadClass("com.jdk.learn.jvm._008ClassLoader").newInstance();
        System.out.println(o.getClass());
        System.out.println(o instanceof _008ClassLoader);
    }
}
